//app.js
var userinfo;
var formid;
var session;
var openId;
var url = require('utils/url.js');
var base64 = require('utils/base64.js');
var token;
App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
  },
  globalData: {
    userInfo: null
  },
  onGotUserInfo1: function (e, that,formid) {
    console.log(this)
    wx.showToast({
      title: '登录中',
      icon: 'loading',
    })
    wx.login({
      success: function (response) {
        var code = response.code
        console.log(response)
        if (response.code) {
          wx.getUserInfo({
            success: function (resp) {
              var nickName = resp.userInfo.nickName;
              var avatarUrl = resp.userInfo.avatarUrl;
              wx.request({
                url: url.URl + 'login/mini',
                data: { code: code },
                method: 'POST',
                success: function (res) {

                  console.log(res.data)

                  userinfo = resp.userInfo;
                  var jsonopenId;
                  var basepenId
                  openId = res.data.data.openid;

                  let data = {};
                  var openID = "openid";
                  let from = "from"
                  var type = "type";
                  var mini_formid = "mini_formid";
                  var nickName1 = "nickName";
                  var avatarUrl1 = "avatar";
                  data[openID] = openId;
                  data[nickName1] = nickName;
                  data[type] = "mini";
                  data[from] = "mini";
                  data[avatarUrl1] = avatarUrl;
                  data[mini_formid] = formid;
                  console.log(data)
                  jsonopenId = JSON.stringify(data)
                  // console.log(jsonopenId)
                  basepenId = base64.Base64.encode(jsonopenId)
                  console.log(basepenId)
                  var session = res.data.data.session_key;
                  var enBasepenId = encodeURIComponent(basepenId)
                  console.log(enBasepenId)
                  wx.request({
                    url: url.URl + 'login',
                    data: { ihy: enBasepenId },
                    method: 'POST',
                    success: function (res) {
                      console.log(res)
                      if (res.data.code == 200) {
                        // console.log(res)
                        var msg = res.data.data
                        var userinfo = res.data.data.userInfo;
                        token = res.data.data.token;
                        that.setData({
                          userinfo: userinfo
                        })
                        wx.setStorage({
                          key: 'userinfo',
                          data: userinfo,
                        })
                        wx.setStorage({
                          key: 'token',
                          data: token,
                        })

                        
                       wx.hideToast()
                        wx.showToast({
                          title: '登录成功',
                          icon: 'success',
                          duration: 2000,
                          success:function(){
                            that.onShow()   
                            if (userinfo.phone==null){
                              wx.showModal({
                                title: '绑定手机号',
                                content: '方便与客服联系',
                                showCancel: true,
                                cancelText: '暂不绑定',
                                confirmText: '去绑定',
                                confirmColor: '#DD2626',
                                success: function (res) {
                                  if (res.confirm) {
                                    wx.navigateTo({
                                      url: '/pages/my_setPhone/my_setPhone',
                                    })
                                  }
                                },
                                fail: function (res) { },
                                complete: function (res) { },
                              })
                            }
                          }
                        })
                      }else{
                        wx.showToast({
                          title: msg,
                          icon: 'success',
                          duration: 2000
                        })
                      }
                    }
                  })
                }
              })

            }, fail: function () {
              console.log("用户拒绝授权")
              wx.hideLoading()
            }
          })
        } else {
          console.log('获取用户登录态失败！' + response.errMsg)
        }
      }
    });
  },

  onGotUserInfo: function (e, that, formid, shareuSer){
    wx.showToast({
      title: '登录中',
      icon: 'loading',
    })
    wx.login({
      success: function (response) {
        var code = response.code
        console.log(response)
        if (response.code) {
          wx.getUserInfo({
            success: function (resp) {
              var nickName = resp.userInfo.nickName;
              var avatarUrl = resp.userInfo.avatarUrl;
              wx.request({
                url: url.URl + 'login/mini',
                data: { code: code },
                method: 'POST',
                success: function (res) {
                  console.log(res.data)
                  userinfo = resp.userInfo;
                  var jsonopenId;
                  var basepenId
                  openId = res.data.data.openid;

                  let data = {};
                  var openID = "openid";
                  let from = "from"
                  var type = "type";
                  var mini_formid = "mini_formid";
                  var nickName1 = "nickName";
                  var avatarUrl1 = "avatar";
                  var shareUser = "share_user";
                  data[openID] = openId;
                  data[nickName1] = nickName;
                  data[type] = "share";
                  data[from] = "mini";
                  data[avatarUrl1] = avatarUrl;
                  data[mini_formid] = formid;
                  data[shareUser] = shareuSer;
                  console.log(data)
                  jsonopenId = JSON.stringify(data)
                  console.log(jsonopenId)
                  basepenId = base64.Base64.encode(jsonopenId)
                  console.log(basepenId)
                  var session = res.data.data.session_key;
                  var enBasepenId = encodeURIComponent(basepenId)
                  console.log(enBasepenId)
                  wx.request({
                    url: url.URl + 'login',
                    data: { ihy: enBasepenId },
                    method: 'POST',
                    success: function (res) {
                      var msg = res.data.data
                      console.log(res)
                      if (res.data.code == 200) {
                        // console.log(res)

                        var userinfo = res.data.data.userInfo;
                        token = res.data.data.token;
                        that.setData({
                          userinfo: userinfo
                        })
                        wx.setStorage({
                          key: 'userinfo',
                          data: userinfo,
                        })
                        wx.setStorage({
                          key: 'token',
                          data: token,
                        })
                        wx.hideToast()
                        wx.showToast({
                          title: '登录成功',
                          icon: 'success',
                          duration: 2000,
                          success: function () {
                            that.onShow()
                            if (userinfo.phone == null) {
                              wx.showModal({
                                title: '绑定手机号',
                                content: '方便与客服联系',
                                showCancel: true,
                                cancelText: '暂不绑定',
                                confirmText: '去绑定',
                                confirmColor: '#DD2626',
                                success: function (res) {
                                  if (res.confirm) {
                                    wx.navigateTo({
                                      url: '/pages/my_setPhone/my_setPhone',
                                    })
                                  }
                                },
                                fail: function (res) { },
                                complete: function (res) { },
                              })
                            }
                          }
                        })
                      } else {
                        wx.showToast({
                          title: msg,
                          icon: 'success',
                          duration: 2000,
                          success: function () {
                          }
                        })
                      }
                    }
                  })
                }
              })

            }, fail: function () {
              console.log("用户拒绝授权")
              wx.hideLoading()
            }
          })
        } else {
          console.log('获取用户登录态失败！' + response.errMsg)
        }
      }
    });
  }
})